import * as Sequelize from "sequelize";
import { Application } from "egg";

interface RoleFuncAttributes {
  id: string;
  role_id: string;
  func_id: string;
  create_user_id: string;
  modify_user_id: string;
  remark?: string;
  create_time: string;
  modify_time: string;
}

type RoleFuncInstance = Sequelize.Instance<RoleFuncAttributes> &
  RoleFuncAttributes;

export default (app: Application) => {
  const { UUID, UUIDV4, DATE, NOW, STRING } = app.Sequelize;
  const attributes: SequelizeAttributes<RoleFuncAttributes> = {
    id: {
      type: UUID,
      primaryKey: true,
      allowNull: false,
      defaultValue: UUIDV4
    },
    role_id: { type: UUID, allowNull: false },
    func_id: { type: UUID, allowNull: false },
    remark: { type: STRING(255), allowNull: true },
    create_user_id: { type: UUID, allowNull: false },
    modify_user_id: { type: UUID, allowNull: false },
    create_time: { type: DATE, allowNull: false, defaultValue: NOW },
    modify_time: { type: DATE, allowNull: false, defaultValue: NOW }
  };
  const model = app.model.define<RoleFuncInstance, RoleFuncAttributes>(
    "RoleFunc",
    attributes,
    {
      tableName: "t_role_func"
    }
  );
  model.associate = () => {
    app.model.models.RoleFunc.belongsTo(app.model.models.Role, {
      foreignKey: "role_id",
      targetKey: "id"
    });
    app.model.models.RoleFunc.belongsTo(app.model.models.Func, {
      foreignKey: "func_id",
      targetKey: "id"
    });
  };
  return model;
};
